﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Shapes;
using System.Windows.Controls;
using System.Windows.Media;
using System.IO;

using SLARToolKit;
using EasyAR.Model;

namespace EasyAR
{
    public class ARServices
    {
        CameraModel cameraModel;
        ARFactory arFactory = new ARFactory();
        Panel viewContainer;
        public ARSilverlightModel ARSLModel { get; private set; }

        public void AddCameraModel(CameraModel cameraModel, Rectangle captureArea)
        {
            this.cameraModel = cameraModel;
            VideoBrush vidBrush = new VideoBrush();
            vidBrush.Stretch = Stretch.Uniform;
            vidBrush.SetSource(cameraModel.Camera);
            captureArea.Dispatcher.BeginInvoke(() =>
            {
                captureArea.Fill = vidBrush;
            });
        }

        public void AddViewContainer(Panel viewContainer)
        {
            this.viewContainer = viewContainer;
        }

        public void AddMarkerAndSLControl(Stream markerStream, string name, int segmentsX, int segmentsY, double width, FrameworkElement element)
        {
            Marker marker = Marker.Load(markerStream, segmentsX, segmentsY, width, name);
            arFactory.AddItem(marker, element);
        }

        public void InitializeARSilverlight(double nearPlane, double farPlane)
        {
            ARSLModel = new ARSilverlightModel(arFactory);
            ARSLModel.InitializeDetector(cameraModel.Camera, nearPlane, farPlane);
            ARSLModel.ViewportContainer = this.viewContainer;
        }

        public void StartAR()
        {
            this.cameraModel.StartCamera();
            ARSLModel.StartARSilverlight();
        }

    }
}
